/**
 * 
 */
package com.jamcodingnm.apps.abqevents.ui;

import java.net.MalformedURLException;
import java.net.URL;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;
import com.jamcodingnm.apps.abqevents.R;
import com.jamcodingnm.apps.abqevents.rss.RSSFactory;
import com.jamcodingnm.apps.abqevents.rss.RSSFeed;
import com.jamcodingnm.apps.abqevents.rss.RSSItem;
import com.jamcodingnm.apps.abqevents.widget.RSSAdapter;


/**
 * @author jeremy
 *
 */
public class FeedFragment extends SherlockListFragment {
	final static String ARG_POSITION = "position";
	int mCurrentPosition = -1;
	String mUrl;
	RSSFeed mRssFeed;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		mUrl = args.getString("RSS_URL");
		refresh();
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		if (getFragmentManager().findFragmentById(R.id.item_fragment) != null) {
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		}
	}
	
	public void refresh() {
		(new RetrieveRssFeedTask()).execute(mUrl);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		RSSItem item = mRssFeed.getItem(position);
		
		Bundle bundle = new Bundle();
		bundle.putString("title", item.getTitle());
		bundle.putString("description", item.getDescription());
		bundle.putString("link", item.getLink());
		bundle.putString("details", item.getDetails());
		
		Intent intent = new Intent(getActivity(), ItemDetailActivity.class);
		intent.putExtras(bundle);
		
		startActivity(intent);
		//getListView().setItemChecked(position, true);
	}
	
	public void updateFeedView(int position) {
		/* 
		 * TODO: While switching between landscape and portrait getActivity() 
		 * will return null and cause a force close.  We check for null here but 
		 * we need to understand why the activity is null for a better fix.
		 */
		if (mRssFeed != null && getActivity() != null) {
			RSSAdapter rssAdapter = new RSSAdapter(getActivity(), mRssFeed.getItemlist());
			setListAdapter(rssAdapter);
		}
	}
	
	/**
     * RetrieveRssFeedTask retrieves the RSS feed from the internet
     * while not running on the main thread which can cause a 
     * NetworkOnMainThreadException when running on main UI thread.
     * 
     * @author jeremy
     */
    class RetrieveRssFeedTask extends AsyncTask<String, Void, RSSFeed> {
    	private Exception exception;
    	private ProgressDialog progressDialog;
    	/*
    	public RetrieveRssFeedTask() {
    		this.progressDialog = new ProgressDialog(getActivity());
    	}
    	
    	@Override
		protected void onPreExecute() {
    		this.progressDialog.setMessage("Loading");
			this.progressDialog.show();
		}*/

		protected RSSFeed doInBackground(String... urls) {
    		URL url = null;
    		
            try {
            	url = new URL(urls[0]);
            } catch (MalformedURLException e) {
            	this.exception = e;
            }
            
            if (url != null) {
            	mRssFeed = RSSFactory.newInstance(url);
            	Log.i("RetrieveRssFeedTask", "-- Finished --");
            }
            
            return mRssFeed;
    	}
    	
    	/* (non-Javadoc)
    	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
    	 */
    	protected void onPostExecute(RSSFeed feed) {
    		if (exception == null) {
    			updateFeedView(-1);
     		} else {
    			Log.i("RetrieveRssFeedTask", exception.getMessage());
    		}
    		/*
    		if (progressDialog.isShowing()) {
    			progressDialog.dismiss();
    		}*/
    	}
    }
}

